package com.bonc.modules.rpt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.factory.PageFactory;
import com.bonc.modules.item.utils.ChartDate;
import com.bonc.modules.item.utils.ChartDateUtils;
import com.bonc.modules.rpt.dao.SuddenEventDao;
import com.bonc.modules.rpt.entity.SuddenEventEntity;
import com.bonc.modules.rpt.param.SuddenEventParam;
import com.bonc.modules.rpt.result.SuddenEventStatResult;
import com.bonc.modules.rpt.result.SurveyResult;
import com.bonc.modules.rpt.result.SurveyResultY;
import com.bonc.modules.rpt.service.SuddenEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jinlong
 * @date 2021/8/4
 */
@Service
@DataSource("data")
public class SuddenEventServiceImpl extends ServiceImpl<SuddenEventDao, SuddenEventEntity> implements SuddenEventService {

    @Autowired
    private SuddenEventDao suddenEventDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveBatch(Collection<SuddenEventEntity> entityList, int batchSize) {
        String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);
        return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
    }

    @Override
    public Page<SuddenEventEntity> page(SuddenEventParam suddenEventParam) {
        LambdaQueryWrapper<SuddenEventEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(suddenEventParam)) {
            if (ObjectUtil.isNotEmpty(suddenEventParam.getEventMonth())) {
                queryWrapper.eq(SuddenEventEntity::getEventMonth, suddenEventParam.getEventMonth());
            }
        }
        queryWrapper.orderByDesc(SuddenEventEntity::getEventMonth, SuddenEventEntity::getEventDay);
        return this.baseMapper.selectPage(PageFactory.defaultPage(), queryWrapper);
    }

    @Override
    public List<SuddenEventEntity> list(SuddenEventParam suddenEventParam) {
        LambdaQueryWrapper<SuddenEventEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(suddenEventParam)) {
            if (ObjectUtil.isNotEmpty(suddenEventParam.getEventMonth())) {
                queryWrapper.eq(SuddenEventEntity::getEventMonth, suddenEventParam.getEventMonth());
            }
        }
        queryWrapper.orderByDesc(SuddenEventEntity::getEventMonth, SuddenEventEntity::getEventDay);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public int delete(SuddenEventParam suddenEventParam) {
        return this.baseMapper.deleteById(suddenEventParam.getId());
    }

    @Override
    public SurveyResult suddenEventStat(String date){
        SuddenEventParam suddenEventParam = new SuddenEventParam();
        SurveyResult surveyResult = new SurveyResult();

        List<String> dateListQuery = new ArrayList<String>();
        List<SuddenEventStatResult> result = null;
        if(date.length() == 10){
            List<ChartDate> dateList = ChartDateUtils.get30Days(date);
            DateTimeFormatter dateTimeFormatterFrom = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            DateTimeFormatter dateTimeFormatterTo = DateTimeFormatter.ofPattern("yyyy年MM月d日");
            for(int i = dateList.size() - 1; i >= 0; i--){
                LocalDate localDate = LocalDate.parse(dateList.get(i).toString(), dateTimeFormatterFrom);
                dateListQuery.add(localDate.format(dateTimeFormatterTo));
            }
            result = suddenEventDao.selectSuddenEventStatByDay(dateListQuery);
        } else if(date.length() == 7){
            List<ChartDate> dateList = ChartDateUtils.get12Month(date);
            for(int i = dateList.size() - 1; i >= 0; i--){
                String str =dateList.get(i).toString().toString().replace("-", "年") + "月";
                dateListQuery.add(str);
            }
            result = suddenEventDao.selectSuddenEventStatByMonth(dateListQuery);
        } else {
            return surveyResult;
        }
        System.out.println(result);
        Map<String, Integer> resultMap = result.stream().collect(Collectors.toMap(SuddenEventStatResult::getEventDate, suddenEventStatResult -> suddenEventStatResult.getCnt()));
        SurveyResultY surveyResultY = new SurveyResultY();
        for(int i = dateListQuery.size() - 1; i >= 0; i--){
            surveyResult.getXAxis().add(dateListQuery.get(i));
            surveyResultY.getData().add(resultMap.get(dateListQuery.get(i)) == null ? "0" : resultMap.get(dateListQuery.get(i)).toString());
        }
        surveyResult.getYAxis().add(surveyResultY);
        return surveyResult;
    }
}
